package com.github.iot.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;

import com.github.iot.R;
import com.github.iot.dao.CommonDaoUtils;
import com.github.iot.dao.DaoUtilsStore;
import com.github.iot.entity.Door;
import com.github.iot.utils.BarUtils;
import com.github.iot.utils.FastJsonUtils;
import com.github.iot.utils.KeyboardUtils;
import com.github.iot.utils.ToastUtils;
import com.github.iot.view.RxTitle;
import com.github.iot.view.dialogs.fragment.SimpleDialogFragment;
import com.github.iot.view.dialogs.iface.ISimpleDialogListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 管理柜门Activity
 *
 * @Author: 梁声洪
 * @Date: 2019/12/29 9:14
 * @Version: v1.0.0
 */
public class DoorUpdateActivity extends AppCompatActivity implements ISimpleDialogListener {

    private static final int REQUEST_SIMPLE_DIALOG = 1;

    @BindView(R.id.rx_title)
    RxTitle mTitle;

    @BindView(R.id.et_type)
    AppCompatEditText mType;

    @BindView(R.id.et_type_name)
    AppCompatEditText mTypeName;

    @BindView(R.id.et_address)
    AppCompatEditText mAddress;

    @BindView(R.id.et_name)
    AppCompatEditText mName;

    @BindView(R.id.et_down_close)
    AppCompatEditText mDownClose;

    @BindView(R.id.et_up_close)
    AppCompatEditText mUpClose;

    @BindView(R.id.et_down_open)
    AppCompatEditText mDownOpen;

    @BindView(R.id.et_up_open)
    AppCompatEditText mUpOpen;

    @BindView(R.id.et_sort)
    AppCompatEditText mSort;

    private static Door door;

    private static CommonDaoUtils<Door> doorDaoUtils = DaoUtilsStore.getInstance().getDoorDaoUtils();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarUtils.setStatusBarColor(this, R.color.colorPrimaryDark);
        setContentView(R.layout.activity_door_update);

        ButterKnife.bind(this);

        door = FastJsonUtils.strToBean(getIntent().getStringExtra("door"), Door.class);

        initView();
        initData();
    }

    private void initView() {
        mTitle.setLeftFinish(this);
        mType.setKeyListener(null);

        mTitle.setTitle(door.getName());
    }

    private void initData() {
        Log.i("LSH", "initData: " + FastJsonUtils.obj2JsonStr(door));
        mType.setText(door.getType());
        mTypeName.setText(door.getTypeName());
        mName.setText(door.getName());
        mAddress.setText(String.valueOf(door.getAddress()));
        mDownClose.setText(String.valueOf(door.getDownClose()));
        mUpClose.setText(String.valueOf(door.getUpClose()));
        mDownOpen.setText(String.valueOf(door.getDownOpen()));
        mUpOpen.setText(String.valueOf(door.getUpOpen()));
        mSort.setText(String.valueOf(door.getSort()));
    }

    private void doorOperation() {
        String typeName = mTypeName.getText().toString();
        String name = mName.getText().toString();
        String address = mAddress.getText().toString();
        String downClose = mDownClose.getText().toString();
        String upClose = mUpClose.getText().toString();
        String downOpen = mDownOpen.getText().toString();
        String upOpen = mUpOpen.getText().toString();
        String sort = mSort.getText().toString();

        if (TextUtils.isEmpty(typeName)) {
            ToastUtils.warning("控制类型名称不能为空");
            return;
        }

        if (TextUtils.isEmpty(address)) {
            ToastUtils.warning("控制地址不能为空");
            return;
        }

        if (TextUtils.isEmpty(name)) {
            ToastUtils.warning("名称不能为空");
            return;
        }

        if (TextUtils.isEmpty(downClose)) {
            ToastUtils.warning("关闭按钮按下指令不能为空");
            return;
        }

        if (TextUtils.isEmpty(upClose)) {
            ToastUtils.warning("关闭按钮松开指令不能为空");
            return;
        }

        if (TextUtils.isEmpty(downOpen)) {
            ToastUtils.warning("打开按钮按下指令不能为空");
            return;
        }

        if (TextUtils.isEmpty(upOpen)) {
            ToastUtils.warning("打开按钮松开指令不能为空");
            return;
        }

        if (TextUtils.isEmpty(sort)) {
            ToastUtils.warning("排序不能为空");
            return;
        }

        door.setTypeName(typeName);
        door.setName(name);
        door.setAddress(Integer.valueOf(address));
        door.setDownClose(Integer.valueOf(downClose));
        door.setUpClose(Integer.valueOf(upClose));
        door.setDownOpen(Integer.valueOf(downOpen));
        door.setUpOpen(Integer.valueOf(upOpen));
        door.setSort(Integer.valueOf(sort));
        doorDaoUtils.update(door);

        ToastUtils.success("操作成功");
    }

    @OnClick({R.id.btn_door})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.btn_door) {
            KeyboardUtils.hideSoftInput(DoorUpdateActivity.this);
            SimpleDialogFragment.createBuilder(DoorUpdateActivity.this, getSupportFragmentManager())
                    .setTitle("系统提示")
                    .setMessage("您确定要操作吗？")
                    .setPositiveButtonText("确定")
                    .setNegativeButtonText("取消")
                    .setRequestCode(REQUEST_SIMPLE_DIALOG)
                    .setCancelable(false)
                    .show();
        }
    }

    @Override
    public void onNegativeButtonClicked(int requestCode) {

    }

    @Override
    public void onNeutralButtonClicked(int requestCode) {

    }

    @Override
    public void onPositiveButtonClicked(int requestCode) {
        if (requestCode == REQUEST_SIMPLE_DIALOG) {
            doorOperation();
        }
    }

}
